//--------------------------------------------------------------------------------------------------------------------
// register the functions
var newRegistery1 = new Array( "getUnsettledContracts", "fillContractsPickList" );
var newRegistery2 = new Array( "getSelectedContractInfo", "displaySettlementSection" );
allJsonsRegistry = allJsonsRegistry.concat(newRegistery1); 
allJsonsRegistry = allJsonsRegistry.concat(newRegistery2); 

//--------------------------------------------------------------------------------------------------------------------
// this function fills the pick list with the betroom
function fillContractsPickList( contractList )
{
    // create reference to first select
    sel1 = document.getElementById("id_contractDropList");

    //get the total item from the dropDownList   
    var intTotalItems = sel1.options.length;

    //loop through the number of items and remove them
    for(var intCounter=intTotalItems;intCounter>=0;intCounter--)
    {
        sel1.remove(intCounter);
    }

    // create the text which indicate what do the option
    sel1.options[sel1.length++] = new Option("Select a contract", "Select a contract");
   
    // fill the first dropdown uninvested stategies
     for (var i = 0; i < contractList.contracts.length; i++)
     {
        var contractName = contractList.contracts[i].name;
        sel1.options[sel1.length++] = new Option( contractName, contractName );
     }
};

//--------------------------------------------------------------------------------------------------------------------
// this notify the server that the user has last visited the specified contract so that it will send the correct information at the request
function requestSelectedContract()
{
    return submitWithoutReload( 
        '/requestSelectedContract/',
        $('#id_SelectContract'),
        '#id_messageDiv',
        ''
        );
};

//--------------------------------------------------------------------------------------------------------------------
// this notify the server that the user has last visited the specified contract so that it will send the correct information at the request
function settleContract()
{
    return submitWithoutReload( 
        '/admin2/settleContract/',
        $('#id_settleContractGroup'),
        '#id_settleContractMsg',
        ''
        );
};


//--------------------------------------------------------------------------------------------------------------------
// put all the radio buttons linked to the settlemnent at the same value
function prepareContractSettlement(input)
{
    if(input == 0){
        document.getElementById('id_settleGroup1').value = 'doNotPay';
        document.getElementById('id_settleGroup2').value = 'doNotPay';
        document.getElementById('id_settleGroup3').value = 'doNotPay';
    }
    if(input == 1){
        document.getElementById('id_settleGroup1').value = 'pay';
        document.getElementById('id_settleGroup2').value = 'pay';
        document.getElementById('id_settleGroup3').value = 'pay';
    }
    else {
        document.getElementById('id_settleGroup1').value = 'refund';
        document.getElementById('id_settleGroup2').value = 'refund';
        document.getElementById('id_settleGroup3').value = 'refund';
    }

}

//--------------------------------------------------------------------------------------------------------------------
// Display the infrmation related to the seleced contract.
function displaySettlementSection( contractInfo ) 
{

    $("#id_selectedContract").html("");
	var generalInfo = contractInfo.generalInfo[0];
	if( typeof(generalInfo.empty) != 'undefined' )
	{ 
	    return;
	}
	
	$("#id_selectedContract").addClass("section");
	$("#id_selectedContract").append('<h2>Settle '+ generalInfo.name+'</h2>');
	if( generalInfo.settlementStatus > -1 )
	{ 
	    $("#id_selectedContract").append('<div class="oneLine2" style="text-align:left;">This contract is now settled.</div>' );
	    return;
	}
	//- FIRST BLOCK
	$("#id_selectedContract").append('<div class="oneLine2" style="text-align:left;" id="id_isContractFixed"></div>' );
	$("#id_isContractFixed").append('<div style="font-weight:bold;">Is Contract fixed?</div> ' );
	var tmpStr = '<div>You can start to say that a contract has fixed. It will be tradle no longer. An expired Contract has to be fixed</div>'; 
	$("#id_isContractFixed").append( tmpStr );    
    tmpStr = '<input type="radio" name="fixingGroup" id="id_fixingGroup1" value="fix" onclick="fixOrUnfixContract(0);" >Fixed<br>';
    $("#id_isContractFixed").append( tmpStr );
    tmpStr = '<input type="radio" name="fixingGroup" id="id_fixingGroup2" value="unfix" onclick="fixOrUnfixContract(1);" >Not fixed<br>';
    $("#id_isContractFixed").append( tmpStr );
    if(generalInfo.settlementStatus > -2) {
        document.getElementById('id_fixingGroup1').checked = true;
    }
    else{
        document.getElementById('id_fixingGroup2').checked = true;
    }
    $("#id_isContractFixed").append('<div id="id_isContractFixedMsg" style="color:red;"/>');

	
	//- SECOND BLOCK
	$("#id_selectedContract").append('<div class="oneLine2" style="text-align:left;" id="id_settleContractGroup"></div>' );
	$("#id_settleContractGroup").append('<div style="font-weight:bold;" >Please indicate the settlement type:</div>' );
	tmpStr = '<input type="radio" name="settleGroup" id="id_settleGroup1" value="doNotPay" onclick="prepareContractSettlement(0);" >The contract holders receive nothing<br>';
    $("#id_settleContractGroup").append( tmpStr );
    tmpStr = '<input type="radio" name="settleGroup" id="id_settleGroup2" value="pay" onclick="prepareContractSettlement(1);">The contract holders receive 100 chochoros<br>';
    $("#id_settleContractGroup").append( tmpStr );
    tmpStr = '<input type="radio" name="settleGroup" id="id_settleGroup3" value="refund" onclick="prepareContractSettlement(2);">The buyer are refunded (this should be avoided as far as possible)<br>';
    $("#id_settleContractGroup").append( tmpStr );
    tmpStr = ' <div>When settling a contract, it will not be tradable exept if you revert the data base.';
    tmpStr += ' If you are not sure which settlement type should be used, just declare the contract as fixed and lead further investigations.';
    tmpStr += ' Settling a contract will save the core information to files so that going to the "save and create" tab and pressing "create base"';
    tmpStr += ' should revert you back to the state before the settlement. However, all the order book and the non core information may be lost.';
    tmpStr += ' PLease use with care!!!!</div>';
    $("#id_settleContractGroup").append( tmpStr );
    $("#id_settleContractGroup").append( '<input class="myRoundCorners" type="submit" value="Settle contract!" onclick="settleContract();"/>' );
    $("#id_settleContractGroup").append( '<div id="id_settleContractMsg" style="color:Red;"></div>' );

}


//--------------------------------------------------------------------------------------------------------------------
// sell to the highest bid
function fixOrUnfixContract(input)
{
    if(input == 0){
        document.getElementById('id_fixingGroup1').checked = true;
        document.getElementById('id_fixingGroup2').value = 'fix';
    }
    else{
        document.getElementById('id_fixingGroup2').checked = true;
        document.getElementById('id_fixingGroup1').value = 'unfix';
        
    }
   return submitWithoutReload( 
    '/admin2/fixOrUnfixContract/',
    $('#id_isContractFixed'),
    '#id_isContractFixedMsg',
    '',
    function(){
                $('#id_isContractFixedMsg').text( 'The status of the contract has been successfully changed.' );
                setTimeout( function() { $('#id_isContractFixedMsg').fadeOut(); }, 3000 );
                return false; 
             }
    );
}